<?php

class UsersController extends AppController {
    
    public $layout = 'administrator';
    public $filtros = array('User.username' => 'Usuario');
    public $head = array('User.username' => 'Usuario');
    
    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('login', 'logout');
    }
    
    public function login() {
        $this->layout = 'login';
        
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                $this->redirect($this->Auth->redirect());
            } else {
                $this->Session->setFlash(__('Usuario y contraseña incorrectos'));
            }
        }
    }

    public function logout() {
        $this->redirect($this->Auth->logout());
    }

    public function index() {
        $this->User->recursive = 0;
        if ($this->request->is('post')) {
            $this->paginate = array(
                'conditions' => arrayFiltro($this->request->data)
            );
        }
        $this->set('filtros', $this->filtros());
        $this->set('filtros_activos', $this->filtrosActivos());
        $this->set('head', $this->head);
        $this->set('rows', $this->paginate());
    }

    public function view($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Usuario inexistente'));
        }
        $this->set('user', $this->User->read(null, $id));
    }

    public function add() {
        if ($this->request->is('post')) {
            //acomodando los datos antes de guardar en la base de datos
            $this->request->data['User']['fecha_carga'] = date("Y-m-d H:i:s");
            
            $this->User->create();
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('Usuario ingresado correctamente'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('El usuario no pudo ser ingresado'));
            }
        }
        $this->set('sectors', $this->User->Sector->find('list'));
        $this->set('rols', $this->User->Rol->find('list'));
    }

    public function edit($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Usuario inexistente'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            //acomodando los datos antes de guardar en la base de datos
            $this->request->data['User']['fecha_modificacion'] = date("Y-m-d H:i:s");
            
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('Usuario editado correctamente'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('El usuario no pudo ser editado'));
            }
        } else {
            $this->request->data = $this->User->read(null, $id);
            unset($this->request->data['User']['password']);
        }
        $this->set('sectors', $this->User->Sector->find('list'));
        $this->set('rols', $this->User->Rol->find('list'));
    }

    public function delete($id = null) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Usuario inexistente'));
        }
        if ($this->User->delete()) {
            $this->Session->setFlash(__('Usuario eliminado correctamente'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('El usuario no pudo ser eliminado'));
        $this->redirect(array('action' => 'index'));
    }

}